﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using System.IO;
using BusinessObjects;

public partial class controls_admin_AdvertisingEdit : Pages_Base
{
    private int aid;
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        aid = getRequest("aid", -1);
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            base.loadModuleByType(ddlModule, 2);
            base.loadModuleByType(ddlModule, 5);
            base.loadModuleByType(ddlModule, 6);
            if (ddlModule.Items.Count == 0)
                ddlModule.Items.Add(new ListItem("Chưa có module quảng cáo nào", "-1"));
            loadAdv();
        }
    }
    private void loadAdv()
    {
        try
        {
            base.OpenConnection();
            objDT = BO_Ads_Picture.Search(aid);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                if (!objDT.Rows[0]["URL"].ToString().Equals(string.Empty))
                {
                    string extension = Path.GetExtension(objDT.Rows[0]["URL"].ToString());
                    if (extension == ".swf")
                        imgAdv.InnerHtml = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"../upload/ads/" + objDT.Rows[0]["URL"].ToString() + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"../upload/ads/" + objDT.Rows[0]["URL"].ToString() + "\"></object>";
                    else imgAdv.InnerHtml = "<img src='../upload/ads/" + objDT.Rows[0]["URL"].ToString() + "' alt='" + objDT.Rows[0]["PICTURE_NAME_VN"].ToString() + "' height='100' onerror='ImgError(this);' />";
                }
                txtTitle.Text = objDT.Rows[0]["PICTURE_NAME_VN"].ToString();
                txtUrl.Text = objDT.Rows[0]["TARGET_LINK"].ToString();
                txtExpired.Text = O2S_DateTime.MM_dd_YYYY(Convert.ToDateTime(objDT.Rows[0]["END_DATE"]));
                chbDisplay.Checked = Convert.ToBoolean(objDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"]);
                txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
                ddlModule.SelectedValue = objDT.Rows[0]["MODULE_ID"].ToString();
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message); }
        finally { base.CloseConnection(); }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        base.OpenConnection();
        objDT = BO_Ads_Picture.Search(aid);
        string fileName = String.Empty;
        fileName = objDT.Rows[0]["URL"].ToString();
        try
        {
            if (fulAdv.HasFile)
            {
                fileName = fulAdv.FileName;
                int count = 0;
                while (File.Exists(Server.MapPath("~/upload/ads/") + fileName))
                {
                    fileName = count + "_" + fulAdv.FileName;
                    count++;
                }
                fulAdv.SaveAs(Server.MapPath("~/upload/ads/") + fileName);
            }
        }
        catch (Exception)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi ghi file!");
            return;
        }
        try
        {
            string title = txtTitle.Text.Trim();
            string url = txtUrl.Text.Trim();
            int module_id = Convert.ToInt32(ddlModule.SelectedValue);
            bool display = chbDisplay.Checked;
            int sortDisplay = Convert.ToInt32(txtSortDisplay.Text);
            DateTime endDate;
            DateTime.TryParse(txtExpired.Text.Trim(), out endDate);
            DataTable admin = (DataTable)Session["Admin"];
            string result = BO_Ads_Picture.Update(Convert.ToInt32(objDT.Rows[0]["PICTURE_ID"]), module_id, title
                    , fileName, url, display, sortDisplay, Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, endDate, admin.Rows[0]["USER_NAME"].ToString(), true);
            if (result.Equals("0"))
                divMessage.InnerHtml = O2S_Message.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
            {
                base.writeAds(module_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công .");
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(7, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_ADS_DISPLAY":
                    NOTE_ADS_DISPLAY.InnerText = value;
                    break;
                case "NOTE_ADS_ENDDATE":
                    NOTE_ADS_ENDDATE.InnerText = value;
                    break;
                case "NOTE_ADS_LINK":
                    NOTE_ADS_LINK.InnerText = value;
                    break;
                case "NOTE_ADS_MODULE":
                    NOTE_ADS_MODULE.InnerText = value;
                    break;
                case "NOTE_ADS_SORT":
                    NOTE_ADS_SORT.InnerText = value;
                    break;
                case "NOTE_ADS_TITLE":
                    NOTE_ADS_TITLE.InnerText = value;
                    break;
                case "NOTE_ADS_URL":
                    NOTE_ADS_URL.InnerText = value;
                    break;
            }
        }
    }
}
